package com.gexin.controller.xapp.acitivity;

import com.gexin.common.model.ApiResponse;
import com.gexin.core.interceptor.XAppSafeInterceptor;
import com.gexin.core.web.AppController;
import com.gexin.model.activity.CashCouponLog;
import com.gexin.service.memberAcitivty.CashCoupService;
import com.gexin.service.memberAcitivty.IntegralService;
import com.jfinal.aop.Before;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.math.BigDecimal;

@Before(XAppSafeInterceptor.class)//统一错误拦截
@ControllerBind(controllerKey = "/xapp/IntegralController")
public class IntegralController extends AppController {

    private static final IntegralService integralService = new IntegralService();

    /**
     * 积分兑换金币
     */
    public void integralExchangeCoin() {
        String speedIntegral = getPara("speedIntegral","0");
        String authUser = getAuthUser().getId() + "";
        CashCoupService cashCoupService = new CashCoupService();
        ApiResponse apiResponse = cashCoupService.exchangeCoin(authUser, new BigDecimal(speedIntegral));
        renderJson(apiResponse);
        return;
    }


    /**
     * 兑货券兑换商品和金币比例
     */
    public void coinExchangeCoinAndBill() {
        String coin = getPara("speedIntegral");
        CashCoupService cashCoupService = new CashCoupService();
        boolean integer = cashCoupService.isInteger(coin);
        if(!integer){
            renderJson(new ApiResponse(false,"进行兑换的兑换券只能为正整数"));
        }
        BigDecimal coinBd = new BigDecimal(coin);
        Integer authUserId = getAuthUserId();
        ApiResponse apiResponse = cashCoupService.exchangeBillOfShopping(authUserId + "", coinBd);
        renderJson(apiResponse);
        return;
    }

    /**
     * 进行兑换操作
     */
    @Before(Tx.class)
    public void exchangeCoin() {
        String speedIntegral = getPara("billOfShopNum", "0");
        String authUserId = getAuthUserId() + "";
        CashCoupService cashCoupService = new CashCoupService();
        ApiResponse apiResponse = cashCoupService.exchangeCoin(authUserId, new BigDecimal(speedIntegral));
        renderJson(apiResponse);
        return;
    }
}
